<?php
App::uses('AppController', 'Controller');
/**
 * Publishers Controller
 *
 * @property Publisher $Publisher
 * @property PaginatorComponent $Paginator
 */
class PublishersController extends AppController {

/**
 * Components
 *
 * @var array
 */
	public $components = array('Paginator');

/**
 * admin_index method
 *
 * @return void
 */
	public function admin_index() {
		$this->Publisher->recursive = 0;
		$this->set('publishers', $this->Paginator->paginate());
		$this->set('title_layout', 'Nhà phát hành');
	}

/**
 * view method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function admin_view($id = null) {
		if (!$this->Publisher->exists($id)) {
			throw new NotFoundException(__('Invalid publisher'));
		}
		$options = array('conditions' => array('Publisher.' . $this->Publisher->primaryKey => $id));
		$this->set('publisher', $this->Publisher->find('first', $options));
		$this->set('title_layout', 'Xem thông tin nhà phát hành game.');
	}

/**
 * add method
 *
 * @return void
 */
	public function admin_add() {
		if ($this->request->is('post')) {
			$this->request->data['Publisher']['alias'] = $this->get_alias($this->request->data['Publisher']['name']);
			
			$result = $this->uploadFile(APP . 'webroot/img/data/publisher/', 'Publisher', 'logo');
			$save = true;
			if($result['status'] == true) {
				$logo = '/img/data/publisher/' . $result['filename'];
				$this->request->data['Publisher']['logo'] = $logo;
			}else {
				$save = false;
				$this->Session->setFlash('<button class="close" data-dismiss="alert" type="button">×</button>Có lỗi trong việc upload logo, vui lòng thử lại.', 'default', array('class' => 'alert alert-error'), 'bad');
			}
			
			if($save == true){
				$this->Publisher->create();
				if ($this->Publisher->save($this->request->data)) {
					$this->Session->setFlash('<button class="close" data-dismiss="alert" type="button">×</button>Lưu thông tin nhà phát hành game thành công.', 'default', array('class' => 'alert alert-success'), 'good');
					return $this->redirect(array('action' => 'admin_index'));
				} else {
					$this->Session->setFlash('<button class="close" data-dismiss="alert" type="button">×</button>Lưu thông tin nhà phát hành game thất bại.', 'default', array('class' => 'alert alert-error'), 'bad');
				}
			}
			
		}
		$this->set('title_layout', 'Thêm nhà phát hành game mới.');
	}

/**
 * edit method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function admin_edit($id = null) {
		if (!$this->Publisher->exists($id)) {
			throw new NotFoundException(__('Invalid publisher'));
		}
		
		$this->Publisher->validator()->remove('logo');
		if ($this->request->is(array('post', 'put'))) {
			$this->request->data['Publisher']['alias'] = $this->get_alias($this->request->data['Publisher']['name']);
			$save = true;
			if($this->request->data['Publisher']['logo']['name'] != ''){
				$result = $this->uploadFile(APP . 'webroot/img/data/publisher/', 'Publisher', 'logo');
				if($result['status'] == true) {
					$logo = '/img/data/publisher/' . $result['filename'];
					$this->request->data['Publisher']['logo'] = $logo;
				}else {
					$save = false;
					$this->Session->setFlash('<button class="close" data-dismiss="alert" type="button">×</button>Có lỗi trong việc upload logo, vui lòng thử lại.', 'default', array('class' => 'alert alert-error'), 'bad');
				}
			}else {
				unset($this->request->data['Publisher']['logo']);
			}
		
			if($save == true){
				if ($this->Publisher->save($this->request->data)) {
					$this->Session->setFlash('<button class="close" data-dismiss="alert" type="button">×</button>Lưu thông tin nhà phát hành game thành công.', 'default', array('class' => 'alert alert-success'), 'good');
					return $this->redirect(array('action' => 'admin_index'));
				} else {
					$this->Session->setFlash('<button class="close" data-dismiss="alert" type="button">×</button>Lưu thông tin nhà phát hành game thất bại.', 'default', array('class' => 'alert alert-error'), 'bad');
				}
			}
			
		} else {
			$options = array('conditions' => array('Publisher.' . $this->Publisher->primaryKey => $id));
			$this->request->data = $this->Publisher->find('first', $options);
		}
		$this->set('title_layout', 'Sửa thông tin nhà phát hành game');
	}

/**
 * delete method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function admin_delete($id = null) {
		$this->Publisher->id = $id;
		if (!$this->Publisher->exists()) {
			throw new NotFoundException(__('Invalid publisher'));
		}
		$this->request->onlyAllow('post', 'delete');
		if ($this->Publisher->delete()) {
			$this->Session->setFlash('<button class="close" data-dismiss="alert" type="button">×</button>Xóa thông tin nhà phát hành game thành công.', 'default', array('class' => 'alert alert-success'), 'good');
		} else {
			$this->Session->setFlash('<button class="close" data-dismiss="alert" type="button">×</button>Xóa thông tin nhà phát hành game thất bại.', 'default', array('class' => 'alert alert-error'), 'bad');
		}
		return $this->redirect(array('action' => 'admin_index'));
	}}
